<?php

class Index_model extends CI_Model {
    
    function newcandidate() {
        $data['name'] = $this->input->post('name');
        $data['nationality'] = $this->input->post('nationality');
        if($data['nationality']*1 == 6){
            $data['nationality'] = $this->input->post('othernationality');
        }
        $data['gender'] = $this->input->post('gender');
        $data['birthday'] = $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day');
        $data['position'] = $this->input->post('position');
        $data['speciality'] = $this->input->post('speciality');
        $data['companyid'] = $this->input->post('companyid');
        $data['status'] = $this->input->post('status');
        if($data['state']*1 == 1){
            $data['passtype'] = $this->input->post('passtype');
        }else{
            $data['passtype'] = 0;
        }
        $data['comment'] = $this->input->post('comment');
        $data['employeeid'] = $this->session->userdata('user')->id;
        $data['createtime'] = time();
        $data['updatetime'] = $data['createtime'];
        $data['overbeensgp'] = $this->input->post('overbeensgp');
        $data['location'] = $this->input->post('location');
        $this->db->insert('workers', $data);
        return $this->db->insert_id();
    }
    
    public function updatecandidate($id){
        $data['name'] = $this->input->post('name');
        $data['nationality'] = $this->input->post('nationality');
        if($data['nationality']*1 == 6){
            $data['nationality'] = $this->input->post('othernationality');
        }
        $data['gender'] = $this->input->post('gender');
        $data['birthday'] = $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day');
        $data['position'] = $this->input->post('position');
        $data['speciality'] = $this->input->post('speciality');
        $data['companyid'] = $this->input->post('companyid');
        $data['workaddress'] = $this->input->post('workaddress');
        $data['status'] = $this->input->post('status');
        if($data['state']*1 == 1){
            $data['passtype'] = $this->input->post('passtype');
        }else{
            $data['passtype'] = 0;
        }
        $data['comment'] = $this->input->post('comment');
        $data['employeeid'] = $this->session->userdata('user')->id;
        $data['createtime'] = time();
        $data['updatetime'] = $data['createtime'];
        $data['overbeensgp'] = $this->input->post('overbeensgp');
        $data['location'] = $this->input->post('location');
        $this->db->update('workers', $data, array('id' => $id));
    }
    
    /**
     * 
     * @param type $title 名称
     * @param type $path 路劲
     * @param type $associatedid 工人id
     * @param type $associatedtype 关联类型
     * @param type $type 类型
     */
    public function addfile($title,$path,$associatedid,$associatedtype,$type=0){
        $data['title'] = $title;
        $data['path'] = $path;
        $data['associatedid'] = $associatedid;
        $data['associatedtype'] = $associatedtype;
        $data['type'] = $type;
        $this->db->insert('attachments', $data);
    }
    
    public function get_candidate($offset,$num,$order='id desc',$condition,$state){
        $table   = "lfsrlzy_workers";
        $str_sql  = "select * from {$table} where state = {$state} and (name like '%".$condition.
                "%' or position like '%".$condition."%' or speciality like '%".$condition.
                "%')  order by {$order} limit {$offset},{$num}";
        $this->db->like('name', $condition);
        $this->db->where('state',$state);
        $this->db->from('workers');
        return array(
            'total' => $this->db->count_all_results(),
            'res' => $this->db->query($str_sql)->result_array(),
            ); 
    }
    
    public function get_campany($offset,$num,$order='id desc',$condition){
        $table   = "lfsrlzy_company";
        $str_sql  = "select c.*,i.name as industryname from {$table} as c "
        . "left join lfsrlzy_industry as i on i.id = c.industry "
        . "where c.name like '%".$condition."%' and c.type = 1 order by {$order} limit {$offset},{$num}";
        $this->db->like('name', $condition);
        $this->db->where('type', 1);
        $this->db->from('company');
        return array(
            'total' => $this->db->count_all_results(),
            'res' => $this->db->query($str_sql)->result_array(),
            ); 
    }
    
    public function deletecandidate(){
        $id = $this->input->get("id");
        $this->db->delete('workers', array('id' => $id));
    }
    
    public function getcandidatebyid($id){
        $query = $this->db->get_where('workers', array('id' => $id));
        return $query->row_array();
    }
    
    public function getAttachmentsBYAssociatedID($id,$associatedtype='0'){
        $query = $this->db->get_where('attachments', array('associatedid' => $id,'associatedtype'=>$associatedtype));
        return $query->result_array();
    }
    
    public function newcompany(){
        $data['name'] = $this->input->post('name');
        $data['sn'] = $this->input->post('sn');
        $data['uen'] = $this->input->post('uen');
        $data['status'] = $this->input->post('status');
        $data['saleser'] = $this->session->userdata('user')->id;;
        $data['industry'] = $this->input->post('industry');
        $data['person'] = $this->input->post('person');
        $data['position'] = $this->input->post('position');
        $data['number'] = $this->input->post('number');
        $data['email'] = $this->input->post('email');
        $data['type'] = $this->input->post('type');
        $data['comment'] = $this->input->post('comment');
        $data['employeeid'] = $this->session->userdata('user')->id;
        $data['createtime'] = time();
        $data['updatetime'] = $data['createtime'];
        $this->db->insert('company', $data);
        return $this->db->insert_id();
    }
    
    public function get_industry($condition){
        $this->db->like('name', $condition);
        $this->db->from('industry');
        $query = $this->db->get();
        return $query->result_array();
    }
    
    public function newindustry(){
        $data['name'] = $this->input->post('industry');
        $this->db->insert('industry', $data);
        return $this->db->insert_id();
    }
    
    public function deleteindustry(){
        $id = $this->input->get("id");
        $this->db->delete('industry', array('id' => $id));
    }
    
    public function deletecompany(){
        $id = $this->input->get("id");
        $this->db->delete('company', array('id' => $id));
    }
    
    public function getcompanybyid($id){
        $query = $this->db->get_where('company', array('id' => $id));
        return $query->row_array();
    }
    
    public function updatecompany(){
        $data['name'] = $this->input->post('name');
        $data['sn'] = $this->input->post('sn');
        $data['industry'] = $this->input->post('industry');
        $data['person'] = $this->input->post('person');
        $data['position'] = $this->input->post('position');
        $data['number'] = $this->input->post('number');
        $data['email'] = $this->input->post('email');
        $data['comment'] = $this->input->post('comment');
        $data['updatetime'] = time();
        $this->db->update('company', $data, array('id' => $this->input->post('id')));
    }
    
    public function get_intermediary($offset,$num,$order='id desc',$condition){
        $table   = "lfsrlzy_company";
        $str_sql  = "select * from {$table} where 1=1 and name like '%".$condition."%' and type = 2  order by {$order} limit {$offset},{$num}";
        $this->db->like('name', $condition);
        $this->db->where('type', 2);
        $this->db->from('company');
        return array(
            'total' => $this->db->count_all_results(),
            'res' => $this->db->query($str_sql)->result_array(),
            ); 
    }
    
    public function newintermediary(){
        $data['name'] = $this->input->post('name');
        $data['region'] = $this->input->post('region');
        $data['person'] = $this->input->post('person');
        $data['number'] = $this->input->post('number');
        $data['email'] = $this->input->post('email');
        $data['type'] = $this->input->post('type');
        $data['qq'] = $this->input->post('qq');
        $data['comment'] = $this->input->post('comment');
        $data['employeeid'] = $this->session->userdata('user')->id;
        $data['createtime'] = time();
        $data['updatetime'] = $data['createtime'];
        $this->db->insert('company', $data);
    }
    
    public function delete_intermediary(){
        $id = $this->input->get("id");
        $this->db->delete('company', array('id' => $id));
    }
    
    public function updateintermediary(){
        $data['name'] = $this->input->post('name');
        $data['region'] = $this->input->post('region');
        $data['person'] = $this->input->post('person');
        $data['number'] = $this->input->post('number');
        $data['email'] = $this->input->post('email');
        $data['qq'] = $this->input->post('qq');
        $data['comment'] = $this->input->post('comment');
        $data['updatetime'] = time();
        $this->db->update('company', $data, array('id' => $this->input->post('id')));
    }
    
    public function get_houseagency($offset,$num,$order='id desc',$condition){
        $table   = "lfsrlzy_company";
        $str_sql  = "select * from {$table} where 1=1 and name like '%".$condition."%' and type = 3  order by {$order} limit {$offset},{$num}";
        $this->db->like('name', $condition);
        $this->db->where('type', 3);
        $this->db->from('company');
        return array(
            'total' => $this->db->count_all_results(),
            'res' => $this->db->query($str_sql)->result_array(),
            ); 
    }
    
    public function newhouseagency(){
        $data['name'] = $this->input->post('name');
        $data['rent'] = $this->input->post('rent');
        $data['address'] = $this->input->post('address');
        $data['person'] = $this->input->post('person');
        $data['number'] = $this->input->post('number');
        $data['type'] = $this->input->post('type');
        $data['comment'] = $this->input->post('comment');
        $data['employeeid'] = $this->session->userdata('user')->id;
        $data['createtime'] = time();
        $data['updatetime'] = $data['createtime'];
        $this->db->insert('company', $data);
    }
    
    public function delete_houseagency(){
        $id = $this->input->get("id");
        $this->db->delete('company', array('id' => $id));
    }
    
    public function updatehouseagency(){
        $data['name'] = $this->input->post('name');
        $data['address'] = $this->input->post('address');
        $data['person'] = $this->input->post('person');
        $data['number'] = $this->input->post('number');
        $data['comment'] = $this->input->post('comment');
        $data['updatetime'] = time();
        $this->db->update('company', $data, array('id' => $this->input->post('id')));
    }
    
    public function getcompanybytype($type='1',$condition=''){
        $sql = "select c.*,i.name as industryname from lfsrlzy_company c"
                . " left join lfsrlzy_industry as i on i.id = c.industry where c.type = ".$type;
        if($condition != '' && $condition != NULL){
            $sql .= " and (c.name like '%".$condition."%' or c.position like '%".$condition.
                    "%' or c.position like '%".$condition."%' or i.name like '%".$condition.
                    "%' or c.person like '%".$condition."%' or c.number like '%".$condition."%')";
        }
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    
    public function getemptyworkers($condition){
        $table   = "lfsrlzy_workers";
        $str_sql  = "select * from {$table} where name like '%".$condition."%' and id not in (select workerid from lfsrlzy_profile  WHERE workerid IS NOT NULL)";

        return array(
            'res' => $this->db->query($str_sql)->result_array(),
        ); 
    }
    
    public function getAllworkers($condition){
        $table   = "lfsrlzy_workers";
        $str_sql  = "select * from {$table} where name like '%".$condition."%'";
        return array(
            'res' => $this->db->query($str_sql)->result_array(),
        ); 
    }
    
    public function getWorkersByOrderID($id){
        $query = $this->db->get_where('workers', array('associatedtype' => 0,'ordersn' => $id));
        return $query->row_array();
    }
    
    public function getWorkersByProfileID($id){
        $query = $this->db->get_where('workers', array('associatedtype' => 1,'ordersn' => $id));
        return $query->row_array();
    }
    
    public function update($tablename,$data,$id){
        $this->db->update($tablename, $data, array('id' => $id));
    }
    
    /**
     * 清除工人的订单编号
     * 同时删除工人待遇确认书
     * @param type $orderid 订单编号
     */
    public function setNULLByOrderID($orderid){
//        $workers = $this->getWorkersByOrderID($orderid);
//        foreach ($workers as $key => $value) {
//            $this->deleteConfirmationLetter($value['id']);
//        }
        $this->db->update("workers", array('ordersn' => NULL), array('ordersn' => $orderid,'associatedtype' => 0));
    }
    
    public function getworkers($data){
        $str_sql  = "select * from lfsrlzy_workers where name like '%".$data['condition']."%'";
        foreach ($data['search'] as $key => $value) {
            $str_sql.=" and ".$key." = ".$value;
        }
        return $this->db->query($str_sql)->result_array();
    }
    
    public function getconfirmationletterbyworkerid($id){
        $query = $this->db->get_where('attachments', array('workerid' => $id,'type' => 1));
        return $query->row_array();
    }
    
    /**
     * 新增或跟新工人的待遇确认书
     * @param type $resumefile 待遇确认书名称
     * @param type $workerid 工人id
     * @param type $id 待遇确认书id 如果为0则新增
     */
    public function editconfirmationletter($resumefile,$workerid,$id){
        $data['path'] = $resumefile;
        $data['title'] = "Confirmation Letter";
        $data['workerid'] = $workerid;
        $data['type'] = 1;
        if($id*1 == 0){
            $this->db->insert('attachments', $data);
        }else if($id*1 > 0){
            $this->db->update('attachments', $data, array('id' => $id));
        }
    }
    
    public function deleteConfirmationLetter($id){
        $this->db->delete('attachments', array('workerid' => $id,'type' => 1));
    }
    
    public function getByID($tablename,$id){
        $query = $this->db->get_where($tablename, array('id' => $id));
        return $query->row_array();
    }
    
    public function deleteAttachments($id){
        $this->db->delete('attachments', array('id' => $id));
    }
    
    public function getOneByData($data,$tablename){
        $query = $this->db->get_where($tablename, $data);
        return $query->row_array();
    }
    
    public function getAllByData($data,$tablename){
        $query = $this->db->get_where($tablename, $data);
        return $query->result_array();
    }
}

?>
